import Vue from 'vue'
import VueRouter from 'vue-router'
import platform from './platform'
import site from './site'

Vue.use(VueRouter)

const routes = [
  ...platform,
  ...site,
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '*',
    name: '404',
    component: () => import('../views/404.vue')
  }
]

const router = new VueRouter({
  routes
})

const whiteList = ['/login']
// 判断当前用户是否登录 路由前置守卫 全局守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (token) {
    // 去的页面是登录页面，直接跳转首页
    if (whiteList.includes(to.path)) {
      next(from.path)
    } else {
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
  // next 通过 next() 跳转页面
})

export default router
